/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved. 
 */
package com.icinfo.cloud.provider.punish.config.service.impl;

import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.config.dto.*;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigApprovalFlowAutoMapper;
import com.icinfo.cloud.provider.punish.config.mapper.ConfigDocumentApprovalFlowAutoMapper;
import com.icinfo.cloud.provider.punish.config.model.ConfigApprovalFlowAuto;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamMain;
import com.icinfo.cloud.provider.punish.config.model.ConfigDocumentApprovalFlowAuto;
import com.icinfo.cloud.provider.punish.config.service.IConfigApprovalFlowAutoService;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamMainService;
import com.icinfo.cloud.provider.punish.config.vo.ConfigAllRoleInfoVo;
import com.icinfo.cloud.provider.punish.config.vo.ConfigApprovalFlowAutoVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述:  config_approval_flow_auto 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月05日
 */
@Service
public class ConfigApprovalFlowAutoServiceImpl extends MyBatisServiceSupport implements IConfigApprovalFlowAutoService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ConfigApprovalFlowAutoServiceImpl.class);

    @Autowired
    private ConfigApprovalFlowAutoMapper configApprovalFlowAutoMapper;
    @Resource
    private IConfigBusiParamMainService configBusiParamMainService;
    @Resource
    private ConfigDocumentApprovalFlowAutoMapper configDocumentApprovalFlowAutoMapper;

    /**
     * 自动审批配置列表
     * @author liyafeng
     * @date 2022/12/5
      * @param request
     * @return {@link List< ConfigApprovalFlowAutoVo>}
     */
    @Override
    public List<ConfigApprovalFlowAutoVo> getConfigApprovalFlowAutoVoList(ConfigApprovalFlowAutoRequest request) {
        //查询参数配置id
        RelationWorkflowInfoRequest relationWorkflowInfoRequest=new RelationWorkflowInfoRequest();
        relationWorkflowInfoRequest.setApplyFieldCode(request.getTerritoryCode());
        relationWorkflowInfoRequest.setApplyAreaCode(request.getAreaCode());
        List<ConfigBusiParamMain>  configBusiParamMainList =configBusiParamMainService.getConfigBusiParamMainList(relationWorkflowInfoRequest);
        if(ObjectUtils.isEmpty(configBusiParamMainList)){
            throw new BusinessException("未找到相应的参数配置");
        }
        ConfigBusiParamMain configBusiParamMain=configBusiParamMainList.get(0);
        String busiParamId=configBusiParamMain.getId();
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("busiParamId",busiParamId);
        paramMap.put("orgName",request.getOrgName());
        paramMap.put("orgCode",request.getOrgCode());
        List<ConfigApprovalFlowAutoVo> configApprovalFlowAutoVos=configApprovalFlowAutoMapper.selectConfigApprovalFlowAutoVoList(paramMap);
        return configApprovalFlowAutoVos;
    }
    /**
     * 自动审批配置
     * @author liyafeng
     * @date 2022/12/6
      * @param saveDto
     * @return {@link int}
     */
    @Override
    public int saveConfigDocumentApprovalFlowAuto(ConfigDocumentApprovalFlowAutoSaveDto saveDto) {
        UserDTO userDTO= UserUtil.getUserInfo();
        int num=0;
        if(ObjectUtils.isEmpty(saveDto.getConfigDocumentApprovalFlowAutoRelations())){
            return num;
        }
        List<ConfigDocumentApprovalFlowAutoRelation> list=saveDto.getConfigDocumentApprovalFlowAutoRelations();
        String autoId=saveDto.getAutoId();
        if(StringUtils.isBlank(autoId)){
            autoId=StringUtils.uuid();
        }
        String isAutoFlow=Constant.STRING_0;
        for (ConfigDocumentApprovalFlowAutoRelation configDocumentApprovalFlowAutoRelation:list) {
            if(Constant.STRING_1.equals(configDocumentApprovalFlowAutoRelation.getIsAutoFlow())){
                isAutoFlow=Constant.STRING_1;
                break;
            }
        }
        for (ConfigDocumentApprovalFlowAutoRelation configDocumentApprovalFlowAutoRelation:list) {
            if(!StringUtils.isBlank(saveDto.getAutoId())&&!StringUtils.isBlank(configDocumentApprovalFlowAutoRelation.getId())){
                //如果自动配置不为空 先无效
                ConfigDocumentApprovalFlowAuto update=new ConfigDocumentApprovalFlowAuto();
                update.setId(configDocumentApprovalFlowAutoRelation.getId());
                update.setIsDeleted(Constant.STRING_1);
                configDocumentApprovalFlowAutoMapper.updateByPrimaryKeySelective(update);
                //删除自动配置
                configApprovalFlowAutoMapper.deleteByPrimaryKey(saveDto.getAutoId());
            }
            //1.新增自动配置表
            ConfigDocumentApprovalFlowAuto configDocumentApprovalFlowAuto=new ConfigDocumentApprovalFlowAuto();
            BeanUtils.copyProperties(configDocumentApprovalFlowAutoRelation,configDocumentApprovalFlowAuto);
            configDocumentApprovalFlowAuto.setId(StringUtils.uuid());
            configDocumentApprovalFlowAuto.setDocumentCatalogCode(saveDto.getDocumentCatalogCode());
            configDocumentApprovalFlowAuto.setUpdateTime(new Date());
            configDocumentApprovalFlowAuto.setAutoId(autoId);
            configDocumentApprovalFlowAuto.setId(StringUtils.uuid());
            configDocumentApprovalFlowAuto.setUpdateUserId(userDTO.getUserId().toString());
            configDocumentApprovalFlowAuto.setUpdateUserName(userDTO.getRealName());
            configDocumentApprovalFlowAuto.setIsDeleted(Constant.STRING_0);
            num=configDocumentApprovalFlowAutoMapper.insert(configDocumentApprovalFlowAuto);
        }
        //新增配置列表
        ConfigApprovalFlowAuto configApprovalFlowAuto=new ConfigApprovalFlowAuto();
        BeanUtils.copyProperties(saveDto,configApprovalFlowAuto);
        configApprovalFlowAuto.setId(autoId);
        configApprovalFlowAuto.setUpdateTime(new Date());
        configApprovalFlowAuto.setUpdateUserId(userDTO.getUserId().toString());
        configApprovalFlowAuto.setUpdateUserName(userDTO.getRealName());
        configApprovalFlowAuto.setIsAutoFlow(isAutoFlow);
        num=configApprovalFlowAutoMapper.insert(configApprovalFlowAuto);
        return num;
    }
    /**
     *  根据审批流查看所有角色信息
     * @author liyafeng
     * @date 2022/12/11
      * @param approvalFolwId
     * @return {@link ConfigAllRoleInfoVo}
     */
    @Override
    public ConfigAllRoleInfoVo getApprovalRoleInfoByApprovalFolwId(String approvalFolwId) {
        ConfigAllRoleInfoVo vo=new ConfigAllRoleInfoVo();
        List<ConfigAllRoleInfo> configAllRoleInfos=configApprovalFlowAutoMapper.selectApprovalRoleInfoByApprovalFolwId(approvalFolwId);
        vo.setConfigAllRoleInfos(configAllRoleInfos);
        return vo;
    }
}